﻿///<author>Bart Goes</author>
///<datecreated>2012-03-26</datecreated>
///<summary>A class that rasterizes an image.</summary>
///
/// 28-3-2012 15:00 : Some small changes and commentary made by Arjen van Zanten
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace THO7_Team8
{
    public class RasterizedImage
    {
        private Bitmap[] sections;
        private int cols, rows, totalWidth, totalHeight;

        /// <summary>
        /// Returns an array that holds all the section Bitmaps. 
        /// </summary>
        public Bitmap[] Sections
        {
            get { return sections; }
        }

        /// <summary>
        /// Constructor. Instantiates a RasterizedImages object, based on the given Bitmap bmp, cols, and rows.
        /// </summary>
        /// <param name="bmp">Input Bitmap</param>
        /// <param name="cols">Amount of colums the image will be rasterized in</param>
        /// <param name="rows">Amoung of rows the image will be rasterized in</param>
        public RasterizedImage(Bitmap bmp, int cols, int rows)
        {
            Bitmap sectionBmp = new Bitmap(bmp);
            
            this.cols = cols;
            this.rows = rows;

            totalWidth = sectionBmp.Width;
            totalHeight = sectionBmp.Height;

            int sectionWidth = totalWidth / cols;
            int sectionHeight = totalHeight / rows;
            sections = new Bitmap[cols * rows];
            int x = 0, y = 0, colCounter = 0;

            //make the section bitmaps
            for (int i = 0; i < (cols * rows); i++)
            {
                Rectangle rect = new Rectangle(x, y, sectionWidth, sectionHeight);
                sections[i] = CreateSection(sectionBmp, rect);
                x += sectionWidth;
                colCounter++;
                if (colCounter == cols)
                {
                    y += sectionHeight;
                    x = 0;
                    colCounter = 0;
                }
            }
        }

        /// <summary>
        /// Creates a new section object from the given Bitmap and Rectangle.
        /// </summary>
        /// <param name="bmpSrc">The source Bitmap</param>
        /// <param name="srcRect">The Rectangle that covers the area the created section will cover</param>
        /// <returns>The created section</returns>
        public Bitmap CreateSection(Bitmap bmpSrc, Rectangle srcRect)
        {
            // Create the new bitmap and associated graphics object
            Bitmap bmp = new Bitmap(srcRect.Width, srcRect.Height);
            Graphics g = Graphics.FromImage(bmp);

            // Draw the specified section of the source bitmap to the new one
            g.DrawImage(bmpSrc, 0, 0, srcRect, GraphicsUnit.Pixel);

            // Clean up
            g.Dispose();

            // Return the bitmap
            return bmp;
        }

        /// <summary>
        /// Restores the sections to a new Bitmap of the original Bitmap size.
        /// </summary>
        /// <returns>The restored Bitmap</returns>
        public Bitmap GetRestoredBitmap()
        {
            Bitmap restoredBitmap = new Bitmap(totalWidth, totalHeight);

            int colsCounter = 0, row = 0;

            foreach (Bitmap bmp in sections)
            {
                using (Graphics g = Graphics.FromImage(restoredBitmap))
                {
                    if (colsCounter == cols)
                    {
                        colsCounter = 0;
                        row++;
                    }
                    g.DrawImage(bmp, colsCounter * bmp.Width, row * bmp.Height);
                    colsCounter++;
                }
            }

            return restoredBitmap;
        }
    }
}
